
package com.shai.wool.common.utils;

import java.beans.PropertyEditorSupport;

import org.apache.commons.lang3.StringEscapeUtils;

/**
 * 字符串过滤特殊字符
 *
 * @author 朱国军
 * @since 1.0
 * @version 2016年4月4日 朱国军
 */
public class StringEscapeEditor extends PropertyEditorSupport {

    private boolean escapeHTML;

    private boolean escapeJavaScript;

    public StringEscapeEditor() {
        super();
    }

    public StringEscapeEditor(boolean escapeHTML, boolean escapeJavaScript) {
        super();
        this.escapeHTML = escapeHTML;
        this.escapeJavaScript = escapeJavaScript;
    }

    @Override
    public void setAsText(String text) {
        if (text == null) {
            setValue(null);
        } else {
            String value = text;
            if (escapeHTML) {
                value = StringEscapeUtils.escapeHtml4(value);
            }
            if (escapeJavaScript) {
                value = StringEscapeUtils.escapeEcmaScript(value);
            }
            setValue(value);
        }
    }

    @Override
    public String getAsText() {
        Object value = getValue();
        return value != null ? value.toString() : "";
    }
}
